package com.example.shop_management.controller;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.shop_management.entity.Category;
import com.example.shop_management.entity.Goods;
import com.example.shop_management.entity.User;
import com.example.shop_management.service.impl.CategoryServiceImpl;
import com.example.shop_management.service.impl.GoodsServiceImpl;
import com.example.shop_management.utils.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsServiceImpl goodsService;

    @Autowired
    private CategoryServiceImpl categoryService;

    @GetMapping("findall")
    public List<Goods> findall() {
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.orderByDesc("create_time");
        List<Goods> list = goodsService.list(goodsQueryWrapper);
        for(Goods goods:list) {
            goods.setCategory(categoryService.getById(goods.getCid()));
        }
        return list;
    }


    @PostMapping("save")
    public boolean insert(@RequestBody Goods goods){
         return goodsService.saveOrUpdate(goods);
    }

    @PostMapping("delete")
    public boolean delete(@RequestBody int id){
        return goodsService.removeById(id);
    }

    @PostMapping("deleltebatch")
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return goodsService.removeByIds(ids);
    }

    //条件查询、
    @GetMapping("selectbycondition")
    public List<Goods> selectByCondition(@RequestParam(defaultValue = "",required = false) String gname,
                                        @RequestParam(defaultValue = "",required = false) String cid,
                                        @RequestParam(defaultValue = "",required = false) String producer){
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        if(!"".equals(gname)){
            goodsQueryWrapper.like("gname",gname);
        }
        if(!"".equals(cid)){
            goodsQueryWrapper.like("cid",cid);
        }
        if(!"".equals(producer)){
            goodsQueryWrapper.like("producer",producer);
        }
        return goodsService.list(goodsQueryWrapper);
    }



}

